package shop.local.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import shop.local.domain.ShopVerwaltung;
import shop.local.valueobjects.Person;
import shop.local.valueobjects.Person.Status;

public class MitarbeiterFormPanel extends JPanel {

	private ShopVerwaltung shop;
	private Eshop eShop;
	private Person user;
	private JFrame frame = new JFrame();
	private JButton registerButton;
	private JLabel anredeLabel;
	private JLabel vornameLabel;
	private JLabel nachnameLabel;
	private JLabel strasseLabel;
	private JLabel plzLabel;
	private JLabel wohnortLabel;
	private JLabel emailLabel;
	private JLabel statusLabel;
	private JLabel gehaltLabel;
	private JLabel benutzernameLabel;
	private JLabel passwortLabel;
	private JComboBox anredeField;
	private JTextField vornameField;
	private JTextField nachnameField;
	private JTextField strasseField;
	private JTextField plzField;
	private JTextField wohnortField;
	private JTextField emailField;
	private JComboBox statusField;
	private JTextField gehaltField;
	private JTextField benutzernameField;
	private JTextField passwortField;

	public MitarbeiterFormPanel(JFrame aFrame , Person user) {

		this.user = user;
		try {
			shop = new ShopVerwaltung("SHOP");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		frame = aFrame;

		initialize();

	}

	private void initialize() {

		registerButton = new JButton();
		registerButton.setText("anlegen");
		registerButton.addActionListener(new RegisterListener());

		anredeLabel = new JLabel("Anrede");
		vornameLabel = new JLabel("Vorname");
		nachnameLabel = new JLabel("Nachname");
		strasseLabel = new JLabel("Strasse Nr.");
		plzLabel = new JLabel("Postleitzahl");
		wohnortLabel = new JLabel("Wohnort");
		emailLabel = new JLabel("eMail");
		statusLabel = new JLabel("Status");
		gehaltLabel = new JLabel("Gehalt");
		benutzernameLabel = new JLabel("Benutzername");
		passwortLabel = new JLabel("Passwort");
		anredeField = new JComboBox();
		vornameField = new JTextField(50);
		nachnameField = new JTextField(50);
		strasseField = new JTextField(50);
		plzField = new JTextField(50);
		wohnortField = new JTextField(50);
		emailField = new JTextField(50);
		statusField = new JComboBox();
		gehaltField = new JTextField(50);
		benutzernameField = new JTextField(50);
		passwortField = new JPasswordField(50);

		statusField.addItem("Admin");
		statusField.addItem("Superadmin");

		anredeField.addItem("Herr");
		anredeField.addItem("Frau");

		JPanel leftPanel = new JPanel();
		JPanel rightPanel = new JPanel();
		JPanel buttonPanel = new JPanel();

		leftPanel.setLayout(new GridLayout(13, 2));
		leftPanel.setPreferredSize(new Dimension(60, 300));
		leftPanel.setBackground(new Color(255, 255, 255));

		rightPanel.setLayout(new GridLayout(13, 2));
		rightPanel.setPreferredSize(new Dimension(100, 300));
		rightPanel.setBackground(new Color(255, 255, 255));

		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.setBackground(new Color(255, 255, 255));

		leftPanel.add(anredeLabel);
		leftPanel.add(vornameLabel);
		leftPanel.add(nachnameLabel);
		leftPanel.add(strasseLabel);
		leftPanel.add(plzLabel);
		leftPanel.add(wohnortLabel);
		leftPanel.add(emailLabel);
		leftPanel.add(benutzernameLabel);
		leftPanel.add(passwortLabel);
		leftPanel.add(statusLabel);
		leftPanel.add(gehaltLabel);

		buttonPanel.add(registerButton);

		rightPanel.add(anredeField);
		rightPanel.add(vornameField);
		rightPanel.add(nachnameField);
		rightPanel.add(strasseField);
		rightPanel.add(plzField);
		rightPanel.add(wohnortField);
		rightPanel.add(emailField);
		rightPanel.add(benutzernameField);
		rightPanel.add(passwortField);
		rightPanel.add(statusField);
		rightPanel.add(gehaltField);
		rightPanel.add(buttonPanel);
		rightPanel.add(new JLabel(""));

		this.setPreferredSize(new Dimension(300, 300));
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.setBorder(BorderFactory.createTitledBorder("Mitarbeiter anlegen"));
		this.setBackground(new Color(255, 255, 255));
		this.add(leftPanel);
		this.add(rightPanel);

	}

	class RegisterListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent ae) {
			if (ae.getSource().equals(registerButton)) {
				int pNr = shop.getNeueMitarbeiterNummer();
				int anr = anredeField.getComponentCount();
				String anrede = "";
				if (anr == 2) {
					anrede = "Herr";
				} else if (anr == 3) {
					anrede = "Frau";
				}
				String vorname = vornameField.getText();
				String nachname = nachnameField.getText();
				String strasse = strasseField.getText();
				String postleitzahl = plzField.getText();
				String wohnort = wohnortField.getText();
				String email = emailField.getText();
				String benutzername = benutzernameField.getText();
				String passwort = passwortField.getText();
				int state = statusField.getComponentCount();
				Status status = null;
				if (state == 2) {
					status = Status.Admin;
				} else if (state == 3) {
					status = Status.Superadmin;
				}
				String gh = gehaltField.getText();
				float gehalt = Float.parseFloat(gh);
				

				boolean ok = shop.fuegePersonEin(pNr, anrede, vorname,
						nachname, strasse, postleitzahl, wohnort, email,
						benutzername, passwort, status, gehalt);
				if (ok) {
					JOptionPane.showMessageDialog(null,
							"Anlegen des Mitarbeiters erfolgreich!\n"
									+ anrede +" " + nachname + " kann sich nun einloggen!", "Erfolgreich",
							JOptionPane.INFORMATION_MESSAGE);
					try {
						shop.schreibePersonen();
						eShop = new Eshop("SHOP", user);
						eShop.user = user;
						frame.dispose();
					} catch (IOException e) {
						JOptionPane.showMessageDialog(null, e.getMessage(),
								"Fehler", JOptionPane.ERROR_MESSAGE);
						e.printStackTrace();
					}
				}
				
			}
		}
	}

}
